package com.spdg.ring.sqlite.service;

import android.content.Context;

import cn.wolf.tools.Log;

import com.spdg.ring.sqlite.dao.impl.ArticleDaoImpl;
import com.spdg.ring.sqlite.model.Article;

public class ArticleService {
	private static ArticleDaoImpl articleDao;
	private static ArticleService articleService;

	private ArticleService(Context context) {
		articleDao = ArticleDaoImpl.newInstance(context);
	}

	public static ArticleService newInstance(Context context) {
		if (articleService == null) {
			articleService = new ArticleService(context);
		}

		return articleService;
	}

	/**
	 * whether article is exist
	 * @param article
	 * @return
	 */
	public boolean isExist(Article article) {
		return articleDao.isExist(article);
	}

	/**
	 * find article by file field key
	 * @param article
	 * @return
	 */
	public Article findByKey(String fileKey) {
		return articleDao.findByKey(fileKey);
	}

	/**
	 * save or update article
	 * @param article
	 */
	public void saveOrUpdate(Article article) {
		if (article == null) {
			Log.e("article is null");
			return;
		}

		if (isExist(article)) {
			articleDao.update(article);
		} else {
			articleDao.insert(article);
		}
	}

	/**
	 * remove article , acorrding to Article's file field
	 * @param article
	 */
	public void remove(Article article) {
		if (article == null) {
			Log.e("article is null");
			return;
		}

		articleDao.delete(article);
	}

	public void removeAll() {
		articleDao.deleteAll();
	}

}
